<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:frbr="http://vocab.org/frbr/core" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:dc="http://purl.org/dc/terms/" exclude-result-prefixes="frbr rdf dc">
    
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:template match="/">
        <resource>
            <xsl:apply-templates select="./*"/>
        </resource>
    </xsl:template>
    
    <xsl:template match="frbr:Manifestation">
        <instance>
        	<xsl:if test="./dc:identifier[@type='voyagerBib']">
            	<xsl:attribute name="id"><xsl:value-of select="./dc:identifier[@type='voyagerBib']" /></xsl:attribute>
            </xsl:if>
            
            <xsl:apply-templates select="@*|node()"/>
        </instance>
    </xsl:template>
    
    <xsl:template match="dc:BibliographicResource[@type='holding']">
        <holding>
            <xsl:attribute name="rdf:about"><xsl:value-of select="./@rdf:about" /></xsl:attribute>
            <xsl:attribute name="available">
            	<xsl:choose>
            		<xsl:when test="./frbr:Item/status/dc:description='Not Charged'">True</xsl:when>
            		<xsl:when test="not(./frbr:item)">True</xsl:when>
            		<xsl:otherwise>False</xsl:otherwise>
            	</xsl:choose>
            </xsl:attribute>
            <xsl:attribute name="type">
            	<xsl:choose>
            		<xsl:when test="not(./frbr:Item) and not(./dc:identifier[@type='call number' and @format='display'])">online</xsl:when>
            		<xsl:otherwise>physical</xsl:otherwise>
            	</xsl:choose>
            </xsl:attribute>
            
            <xsl:for-each select="./frbr:Item">
            	<xsl:sort select="./item_sequence" />
            	<copy>
            		<xsl:call-template name="threshold" />
            		<xsl:apply-templates select="./*"/>
            	</copy>
            </xsl:for-each>
            
            <xsl:apply-templates select="./*" />
        </holding>
    </xsl:template>
    <xsl:template match="dc:Location[@type='library']">
        <library>
            <xsl:value-of select="./dc:title[@type='display']" />
        </library>
    </xsl:template>
    <xsl:template match="dc:Location[not(@type='library')]">
        <location>
            <xsl:copy-of select="@*" />
            <xsl:if test="./dc:title[@type='code']">
            	<xsl:attribute name="code"><xsl:value-of select="./dc:title[@type='code']" /></xsl:attribute>
            </xsl:if>
            <xsl:value-of select="./dc:title[not(@type)]" />
        </location>
        <xsl:apply-templates select="./*"/>
    </xsl:template>
    <xsl:template match="status">
        <status>
        	<xsl:if test="./dc:date">
            	<xsl:attribute name="dc:date"><xsl:value-of select="./dc:date" /></xsl:attribute>
            </xsl:if>
            <xsl:value-of select="./dc:description" />
        </status>
    </xsl:template>
    
    <xsl:template match="dc:identifier[@type='call number' and @format='display']">
    	<callNumber><xsl:value-of select="." /></callNumber>
    </xsl:template>
    
    <xsl:template name="threshold">
    	<xsl:choose>
           	<xsl:when test="dc:coverage[@type='chron'] and enum">
           		<threshold><xsl:value-of select="enum" /> (<xsl:value-of select="dc:coverage[@type='chron']" />)</threshold>
           	</xsl:when>
           	<xsl:when test="dc:coverage[@type='chron']">
           		<threshold><xsl:value-of select="dc:coverage[@type='chron']" /></threshold>
           	</xsl:when>
           	<xsl:when test="enum">
           		<threshold><xsl:value-of select="enum" /></threshold>
           	</xsl:when>
           </xsl:choose>
    </xsl:template>
    
    <xsl:template match="dc:identifier[@type='barcode']"> 
    	<barcode>
    		<xsl:if test="@status">
    			<xsl:attribute name="status"><xsl:value-of select="@status" /></xsl:attribute>
    		</xsl:if>
    		<xsl:value-of select='.' />
    	</barcode>
    </xsl:template>
    
    <xsl:template match="node()|@*" />    
</xsl:stylesheet>